﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using NLBS.Entity;
using System.Data;

namespace NLBS.Data
{
    public partial class SqlDataProvider : DbProvider
    {
        #region UserGroup

        public override IList<UserGroup> GetUserGroup()
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT group_id, group_name, group_rights FROM dbo.blog_UserGroup");

            IList<UserGroup> lists = new List<UserGroup>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    UserGroup group = CommonDataProvider.PopulateUserGroupFromIDataReader(dataReader);
                    lists.Add(group);
                }
            }
            return lists;
        }

        public override int InsertUserGroup(UserGroup group)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"INSERT INTO [dbo].[blog_UserGroup] ([group_name], [group_rights]) VALUES (@group_name, @group_rights);
            SELECT @group_ID=@@IDENTITY");
            db.AddInParameter(dbCommand, "group_Name", DbType.String, group.Name);
            db.AddInParameter(dbCommand, "group_Rights", DbType.String, group.Rights);
            db.AddOutParameter(dbCommand, "group_ID", DbType.Int32, 10);
            db.ExecuteNonQuery(dbCommand);
            return Convert.ToInt32(db.GetParameterValue(dbCommand, "@group_ID"));
        }

        public override void UpdateUserGroup(UserGroup group)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"UPDATE [dbo].[blog_UserGroup] SET 
            [group_name] = @group_name, [group_rights] = @group_rights WHERE (([group_id] = @group_id))");
            db.AddInParameter(dbCommand, "group_Id", DbType.String, group.GroupID);
            db.AddInParameter(dbCommand, "group_Name", DbType.String, group.Name);
            db.AddInParameter(dbCommand, "group_Rights", DbType.String, group.Rights);
            db.ExecuteNonQuery(dbCommand);
        }

        public override UserGroup GetUserGroupByID(int groupID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"SELECT   group_id, group_name, group_rights
            FROM      blog_UserGroup
            WHERE   (group_id = @group_id)");
            db.AddInParameter(dbCommand, "group_id", DbType.Int32, groupID);

            UserGroup group = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    group = CommonDataProvider.PopulateUserGroupFromIDataReader(dataReader);
                }
            }
            return group;
        }

        public override void DeleteUserGroup(int groupID)
        {
            
            DbCommand dbCommand = db.GetSqlStringCommand(@"DELETE FROM [dbo].[blog_User] WHERE (([user_groupID] = @group_id))
            DELETE FROM [dbo].[blog_UserGroup] WHERE (([group_id] = @group_id))");
            db.AddInParameter(dbCommand, "group_Id", DbType.String, groupID);
            db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}
